/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "WLOGR_frame.h"
#include <sys/stat.h>

static int cfr = 1;
static int dfr = 1;
tgrFrame *
new_tgr_frame(GraphFrame *gf)
{
  tgrFrame *tgf = (tgrFrame *)calloc(1, sizeof(tgrFrame));
  if(!tgf) {
      fprintf(stderr,"Out of Memory new_tgr_frame\n");
      abort();
  }
  tgf->gf = gf;
  tgf->saved = 0;
  tgf->root = NULL;
  tgf->broot = NULL;
  printf("New Graph %d\n", cfr++);
  return tgf;
}

void
set_browse_graph_frame(tgrFrame *cgf, tgrFrame *bgf)
{
  cgf->br_gr = bgf;
}

void
set_group_graph_frame(tgrFrame *cgf, tgrFrame *ggf)
{
  cgf->gr_gr = ggf;
}

tgrFrame *
get_browse_graph_frame(tgrFrame *cgf)
{
  return cgf->br_gr;
}

tgrFrame *
get_group_graph_frame(tgrFrame *cgf)
{
  return cgf->gr_gr;
}

int
save_current_graph(tgrFrame *tgf)
{
  int fd,fdb;
  FILE *fp;
  long len,n;
  char *text=NULL, *tstring=NULL;

  char *fileback=NULL,*filename=NULL;
  struct stat statbuf;

  if(!tgf->filename)
    {
      char name[1024];
      printf("New Name for Graph File : ");
      scanf("%s", name);
      GRStrAllocCopy(tgf->filename,name);
      printf("File Name: %s\n", tgf->filename);
    }

  filename = tgf->filename;
  /* Create a Back up file */

  if((fd = open(filename,O_RDONLY,0)) != -1)
   {
      fileback = (char *)malloc((strlen(filename)+2)*sizeof(char));
      if(!fileback) {
	  fprintf(stderr,"Out of Memory save_current_graph\n");
	  abort();
      }
      sprintf(fileback,"%s~",filename);
      if((fdb = creat(fileback,PERMS)) == -1)
      {
	 fprintf(stderr,"Can not create backup file %s\n", filename);
	 return 1;
      }
      if(stat(filename, &statbuf) == 0)
	 len = statbuf.st_size;
      else 
	 len = BUFSIZ;
      tstring = (char *)malloc((len +2)*sizeof(char));
      if(!tstring) {
	  fprintf(stderr,"Out of Memory save_current_graph\n");
	  abort();
      }
      while((n = read(fd, tstring,len)) > 0)
	 if(write(fdb, tstring, n) != n)
	 {
	    fprintf(stderr,"File %s is not saved \n",fileback);
	 }

      close(fdb);
      close(fd);
      free(tstring); tstring = NULL;
      free(fileback); fileback = NULL;
   }
   /* Create the file if it is a new file and write the
      content of the Text Window to the file */
  if(!(fp = fopen(filename,"w")))
   {
      fprintf(stderr,"Can't save current graph\n");
      return -1;
   }

  text= get_xml_text_graph(tgf->gf,&len,GR_STANDARD_FORMAT);

  if(!text)
    return -1;

  if(fwrite(text,sizeof(char), len, fp) != len)
    fprintf(stderr,"Warning: did not write entire file\n");
  else 
    {
      /* make sure a newline terminates file */
      if(text[len-1] != '\n')
	fputc('\n', fp);
      fprintf(stderr,"File saved: %s\n", filename);
    }
  
  tgf->saved = 1;
  free(text); text = NULL;
  fclose(fp); 
  return 0;
}

void
delete_tgr_frame(tgrFrame *tgf)
{
    if(tgf->filename) {
	free(tgf->filename); tgf->filename = NULL;
    }
  if(tgf->gf)
    {
      delete_graph_resources(tgf->gf);
      free(tgf->gf); tgf->gf = NULL;
    }
  printf("Delete FRAME %d\n", dfr++);
  free(tgf); tgf = NULL;
}



